/*
 * Copyright (C) 2016 yixun All Rights Reserved.
 * 
 * ImportStatisticsServiceImpl.java
 */
package com.emm.yixun.basic.service.impl;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.emm.yixun.common.page.Page;
import com.emm.yixun.common.utils.MathUtil;
import com.emm.yixun.common.model.ImportStatistics;
import com.emm.yixun.common.model.ImportStatisticsSummary;
import com.emm.yixun.common.model.dto.CallCustomerDto;
import com.emm.yixun.common.model.dto.ImportStatisticsDto;
import com.emm.yixun.basic.service.ImportStatisticsService;
import com.emm.yixun.basic.mapper.CallCustomerMapper;
import com.emm.yixun.basic.mapper.ImportStatisticsMapper;


/**
 * [文件名称]<br>
 * ImportStatisticsServiceImpl.java<br>
 * <br>
 * [文件描述]<br>
 * 导入统计表 service interface impl<br>
 * <br>
 * [修改记录]<br>
 * 2016-11-15 11:14:13 创建 陶俊代码生成器<br>
 * 
 * @author 陶俊代码生成器
 * @version 1.00
 */
@Service("importStatisticsService")
public class ImportStatisticsServiceImpl implements ImportStatisticsService {
	
	@Autowired
	private ImportStatisticsMapper mapper;
	
	@Autowired
	private CallCustomerMapper callCustomerMapper;
	
	/**
	 * @param entity 搜索条件
	 * @param begin 分页开始行数[不包含]
	 * @param end 分页结束行数[包含]
	 * @return 分页记录
	 */
	@Transactional(readOnly = true)
	public Page<ImportStatistics> getPageModel(ImportStatisticsDto entity,int begin,int end){
		Integer total = mapper.count(entity);
		List<ImportStatistics> rows = mapper.list(entity,begin,end);
	    Page<ImportStatistics> msr = new Page<ImportStatistics>(total, rows);
	    msr.setPageSize(end);
		return msr;
	}

	/**
	 * 查询列表
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public List<ImportStatistics> selectListByDto(ImportStatisticsDto entity){
		return mapper.selectListByDto(entity);
	}
	
	/**
	 * 查询单个
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public ImportStatistics selectByDto(ImportStatisticsDto entity){
		return mapper.selectByDto(entity);
	}
	
	/**
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public ImportStatistics selectById(Long id){
		if(id!=null){
			return mapper.selectById(id);
		}
		return null;
	}
	/**
	 * @param entity 新增记录的详细信息
	 * @return 新增行数
	 */
	public ImportStatisticsDto create(ImportStatisticsDto entity) {
		entity.setCreateTime(Calendar.getInstance().getTime());
		mapper.add(entity);
		return entity;
	}

	/**
	 * @param entity 更新记录的详细信息
	 * @return 更新行数
	 */
	public int update(ImportStatisticsDto entity) {
		entity.setUpdateTime(Calendar.getInstance().getTime());
		return mapper.edit(entity);
	}
    
	/**
	 * @param entity 待删除的记录信息
	 * @return 删除行数
	 */
	public int destroy(ImportStatisticsDto entity) {
		return mapper.del(entity);
	}
	
	/**
	 * @param entity 新增或更新
	 * @return 新增行数
	 */
	public int saveOrUpdate(ImportStatisticsDto entity) {
		if(entity!=null&&entity.getId()!=null){
			return update(entity);
		}else{
			create(entity);
			return 1;
		}
	}
	
	public Map<String, String> findSummaryRate(ImportStatisticsDto importStatisticsDto){
		 Map<String, String> map=new HashMap<String, String>();
		 map.put("successRate", "0");
		 map.put("effectiveRate", "0");
		 //查询导入成功率
		 ImportStatisticsSummary summary=mapper.selectSummary(importStatisticsDto);
		 if(null!=summary){
			 map.put("successRate", summary.getSuccessRate());
		 }
		 //查询转客户率
		 try {
			 CallCustomerDto callCustomerDto=new CallCustomerDto();
			 if(null!=importStatisticsDto.getCreateUserId()){
				 callCustomerDto.setCreateUserId(importStatisticsDto.getCreateUserId());
			 }
			 if(null!=importStatisticsDto.getCreateTimeBefore()){
				 callCustomerDto.setCreateTimeBefore(importStatisticsDto.getCreateTimeBefore());
			 }
			 if(null!=importStatisticsDto.getCreateTimeAfter()){
				 callCustomerDto.setCreateTimeAfter(importStatisticsDto.getCreateTimeAfter());
			 }
			 int totalCount=callCustomerMapper.count(callCustomerDto);
			 callCustomerDto.setStatus("3");
			 int totalSuccessCount=callCustomerMapper.count(callCustomerDto);
			 if(totalCount>0){
				 double rateDouble=MathUtil.divide(new BigDecimal(totalSuccessCount),new BigDecimal(totalCount), 2);
				 String effectiveRate=String.valueOf(MathUtil.multiply(rateDouble, Double.valueOf("100")));
				 map.put("effectiveRate",effectiveRate); 
			 }
			 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 return map; 
	}
	
}
